package com.pignest.hotel.ui.reception;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

import com.pignest.framework.util.Utils;
import com.pignest.hotel.beans.Booking;
import com.pignest.hotel.service.ReceptionService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.system.L;
import com.pignest.hotel.util.DateUtil;
import com.pignest.hotel.util.UIUtils;

/**
 * 修改 预定
 */
public class EditBookingWindow extends Window {
	private static final long serialVersionUID = 961821788123920656L;

	private Booking booking;
	private boolean success;
	
    private Textbox userName;
    private Textbox phoneNumber;
    private Datebox estimatedDate;
    private Listbox preserve;
    private Label endTime;
    private Label bookingRooms;
    private Textbox remark;
    private Textbox memberTextbox;
    
    public EditBookingWindow() {
	}
    
    private void init() {
		userName = (Textbox) getFellow("userName");
		phoneNumber = (Textbox) getFellow("phoneNumber");
		estimatedDate = (Datebox) getFellow("estimatedDate");
		preserve = (Listbox) getFellow("preserve");
		endTime = (Label) getFellow("endTime");
		bookingRooms = (Label) getFellow("bookingRooms");
		remark = (Textbox) getFellow("remark");
		memberTextbox = (Textbox) getFellow("member");
		
		setModBookingInfo();
    }
    
    public boolean isSuccess() {
		return success;
	}
    
	public void setBooking(Booking selectBooking) {
		this.booking = selectBooking;
		init();
	}
	
	public void setModBookingInfo() {
		if(booking!=null) {
			userName.setValue(booking.getName());
			phoneNumber.setValue(booking.getPhone());
			estimatedDate.setValue(booking.getEstimatedTime());
			endTime.setValue(DateUtil.toString(booking.getEndTime(), DateUtil.HOTEL_DEFAULT_FORMAT));
			UIUtils.setSelectedItem(preserve, String.valueOf(booking.getPreserve()));
			bookingRooms.setValue(booking.getBookingRoomNames());
			remark.setValue(booking.getRemark());
			memberTextbox.setValue(booking.getMember() == null ? null : booking.getMember().getDisplayName());
		}
	}
	
	public void save() {
		success = false;
		String error = check();
		if (error != null) {
			UIUtils.alert(error);
			return;
		}
		String name = StringUtils.defaultIfEmpty(userName.getValue(), null);
		String phone = StringUtils.defaultIfEmpty(phoneNumber.getValue(), null);
		Date date = estimatedDate.getValue();
		String _preserve = UIUtils.getSelectedValue(preserve);
		int p = Utils.getInteger(_preserve, 30);
		
		ReceptionService service = Services.getReceptionService();
		Booking b = service.get(Booking.class, booking.getId());
		b.setName(name);
		b.setPhone(phone);
		b.setEstimatedTime(date);
		b.setPreserve(p);
		
		service.update(b);
		L.update(b, "修改预订");
		this.booking = b;
		success = true;
		detach();
	}
	

	public String check() {
		String name = userName.getValue();
		String phone = phoneNumber.getValue();
		if (StringUtils.isEmpty(name) || StringUtils.isEmpty(phone)) {
			userName.focus();
			return ("请输入预订者姓名和电话!");
		}
		Date d = estimatedDate.getValue();
		if (!estimatedDate.isValid() || d == null || d.getTime() < System.currentTimeMillis()) {
			estimatedDate.focus();
			return ("请输入合法的预订时间!, 预订日期晚于现在!");
		}
		return null;
	}
	
	public void calcTime() {
		Date date = estimatedDate.getValue();
		String sPreserve = UIUtils.getSelectedValue(preserve);
		String sEndTime = calcTime(date, Integer.parseInt(sPreserve));
		endTime.setValue(sEndTime);
	}
	
	/**
	 * 计算时间(预订时间 -> 失效时间)
	 * */
	private static String calcTime(Date date ,int delay) {
		if (date == null || delay <= 0)
			return null;
		//Date d = DateUtil.parseDate(date, hms);
		return DateUtil.toString(new Date(date.getTime() + delay * 60L * 1000), DateUtil.HOTEL_DEFAULT_FORMAT);
	}
}
